package MyDisplayObjects
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.dns.AAAARecord;
	import flash.text.*;
	
	/** An object of this class is a button that is shaped to fit a document */
	public class TabButton extends MovieClip
	{
		// private variables
		private var selectedColor:uint, overColor:uint, unselectedColor:uint, textColor:uint, currentColor:uint;
		private var tField:TextField;
		private var faceWidth:Number, faceHeight:Number;
		private var referenceText:String; // create size based on this text
		private var directDeselection:Boolean;
		// variables accessible with a getter
		private var _orientation:String; //{North, South, East, West}
		private var _tabShape:String; // {Rect, Trapezoid, RoundRect}
		private var _mouseOver:Boolean = false;
		private var _selected:Boolean = false;
		// getters
		public function get orientation():String{return _orientation;}
		public function get tabShape():String{return _tabShape;}
		public function get mouseOver():Boolean{return _mouseOver;}
		public function get selected():Boolean{return _selected;}
		
		public function TabButton(faceWidth:Number, faceHeight:Number, orientation:String, tabShape:String, fontSize:Number, text:String, selectedColor:uint, referenceText:String=null, directDeselection:Boolean = false)
		{
			this.faceWidth = faceWidth;
			this.faceHeight = faceHeight;
			this._orientation = orientation;
			this._tabShape = tabShape;
			this.selectedColor = selectedColor;
			this.referenceText = referenceText;
			if (referenceText == null) this.referenceText = text;
			this.directDeselection = directDeselection;
			var r:int, g:int, b:int;
			// get dark color for unselectedColor
			r = selectedColor & 0xFF0000; r = r >> 16;
			g = selectedColor & 0x00FF00; g = g >> 8;
			b = selectedColor & 0x0000FF;
			var dr:int = r-r/8; if (dr < 0) dr = 0;
			var dg:int = g-g/8; if (dg < 0) dg = 0;
			var db:int = b-b/8; if (db < 0) db = 0;
			overColor = dr << 16 | dg << 8 | db;
			// get slightly darker color for over
			dr = r-r/4; if (dr < 0) dr = 0;
			dg = g-g/4; if (dg < 0) dg = 0;
			db = b-b/4; if (db < 0) db = 0;
			unselectedColor = dr << 16 | dg << 8 | db;
			// get dark color for text
			dr = r-r/2; if (dr < 0) dr = 0;
			dg = g-g/2; if (dg < 0) dg = 0;
			db = b-b/2; if (db < 0) db = 0;
			textColor = dr << 16 | dg << 8 | db;
			
			// textField
			tField = new TextField();
			tField.selectable = false;
			tField.mouseEnabled = false;
			// size until text is correct size
			for (var _fontSize:Number = fontSize; _fontSize > 0; _fontSize--)
			{
				tField.defaultTextFormat = new TextFormat ("Arial", _fontSize, textColor);
				tField.text = referenceText;
				tField.autoSize = "left";
				if (tField.textWidth <= faceWidth && tField.textHeight <= faceHeight)
				{
					break;
				}
			}
			tField.text = text;
			//this.graphics.drawRect(0, 0, faceWidth, faceHeight);
			tField.x = (faceWidth - tField.width)/2;
			tField.y = (faceHeight - tField.height)/2;
			this.addChild(tField);
			currentColor = unselectedColor;
			redraw();
			this.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);	
		}
		/////////////////////////////////// INTERACTIVITY ///////////////////////////////////////////
		private function handleMouseOver (event:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);	
			_mouseOver = true;
			//event.stopPropagation();
			if (!selected)
			{
				currentColor = overColor;
				redraw();
			}
			this.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);	
			this.addEventListener(MouseEvent.CLICK, handleClick);	
		}
		private function handleMouseOut (event:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);	
			this.removeEventListener(MouseEvent.CLICK, handleClick);
			event.stopPropagation();
			_mouseOver = false;
			if (!selected)
			{
				currentColor = unselectedColor;
				redraw();
			}
			this.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);		
		}
		private function handleClick (event:MouseEvent):void
		{
			if (selected)
			{
				if (directDeselection)
				{
					_selected = false;
					currentColor = overColor;
					redraw();
					this.dispatchEvent(new Event(Event.CANCEL));
				}
			} else
			{
				_selected = true;
				currentColor = selectedColor;
				redraw();
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
			
		}
		public function select():void
		{
			if (!selected)
			{
				_selected = true;
				currentColor = selectedColor;
				redraw();
			}
			
		}
		/** Remotely de-selects this button */
		public function deselect():void
		{
			if (selected)
			{
				_selected = false;
				if (mouseOver)
				{
					currentColor = overColor;
					redraw();
				} else 
				{					
					currentColor = unselectedColor;
					redraw();				
				}
			}
		}
		
		/////////////////////////////////// DRAW          ///////////////////////////////////////////
		/** Draw based on current color */
		private function redraw():void
		{
			this.graphics.lineStyle(faceWidth/100, 0x888888);
			this.graphics.beginFill(currentColor);
			if (tabShape.toLowerCase() == "rect" || tabShape.toLowerCase() == "rectangle")
			{
				if (orientation.toLowerCase() == "north")
				{
					this.graphics.moveTo(0, faceHeight);
					this.graphics.lineTo(0, 0);
					this.graphics.lineTo(faceWidth, 0);
					this.graphics.lineTo(faceWidth, faceHeight);
				} else if (orientation.toLowerCase() == "south")
				{
					this.graphics.lineTo(0, faceHeight);
					this.graphics.lineTo(faceWidth, faceHeight);
					this.graphics.lineTo(faceWidth, 0);
				} else if (orientation.toLowerCase() == "west")
				{
					this.graphics.moveTo(faceWidth, 0);
					this.graphics.lineTo(0, 0);
					this.graphics.lineTo(0, faceHeight);
					this.graphics.lineTo(faceWidth, faceHeight);
				} else if (orientation.toLowerCase() == "east")
				{
					this.graphics.lineTo(faceWidth, 0);
					this.graphics.lineTo(faceWidth, faceHeight);
					this.graphics.lineTo(0, faceHeight);
				} else if (orientation.toLowerCase() == "center")
				{
					this.graphics.lineTo(0, faceHeight);
					this.graphics.lineTo(faceWidth, faceHeight);
					this.graphics.lineTo(faceWidth, 0);
					this.graphics.lineTo(0, 0);
				}
			} else if (tabShape.toLowerCase() == "roundrect" || tabShape.toLowerCase() == "roundrectangle")
			{
				var ellipseWidth:Number = Math.min(width/4, height/4);
				if (orientation.toLowerCase() == "north")
				{
					this.graphics.moveTo(0, faceHeight);
					this.graphics.lineTo(0, ellipseWidth);
					this.graphics.curveTo(0, 0, ellipseWidth, 0);
					this.graphics.lineTo(faceWidth-ellipseWidth, 0);
					this.graphics.curveTo(faceWidth, 0, faceWidth, ellipseWidth);
					this.graphics.lineTo(faceWidth, faceHeight);
				} else if (orientation.toLowerCase() == "south")
				{
					this.graphics.lineTo(0, this.height-ellipseWidth);
					this.graphics.curveTo(0, this.height, ellipseWidth, this.height);
					this.graphics.lineTo(faceWidth-ellipseWidth, faceHeight);
					this.graphics.curveTo(faceWidth, faceHeight, faceWidth, faceHeight-ellipseWidth);
					this.graphics.lineTo(faceWidth, 0);
				} else if (orientation.toLowerCase() == "west")
				{
					this.graphics.moveTo(0, faceWidth);
					this.graphics.lineTo(0, ellipseWidth);
					this.graphics.curveTo(0, 0, 0, ellipseWidth);
					this.graphics.lineTo(0, faceWidth-ellipseWidth);
					this.graphics.curveTo(0, faceHeight, ellipseWidth, faceHeight);
					this.graphics.lineTo(faceWidth, faceHeight);
				} else if (orientation.toLowerCase() == "east")
				{
					this.graphics.lineTo(this.width-ellipseWidth, 0);
					this.graphics.curveTo(this.width, 0, this.width, ellipseWidth);
					this.graphics.lineTo(faceWidth, faceHeight-ellipseWidth);
					this.graphics.curveTo(faceWidth, faceHeight, faceWidth-ellipseWidth, faceHeight);
					this.graphics.lineTo(0, faceHeight);
				} 
			}
			
			this.graphics.endFill();
		}
	}
}